/*
 */
package com.xtd.view;

import java.io.Serializable;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Dr. Spock (spock at dev.java.net)
 */
public abstract class AbstractManager implements Serializable {

    protected void addMessage(String summary, String detail) {
        addMessage(null, summary, detail, FacesMessage.SEVERITY_INFO);
    }

    protected void addMessage(String componentId, String summary, String detail) {
        addMessage(componentId, summary, detail, FacesMessage.SEVERITY_INFO);
    }

    protected void addMessage(String summary, String detail, Severity severity) {
        addMessage(null, summary, detail, severity);
    }

    protected void addMessage(String componentId, String summary, String detail, Severity severity) {
        FacesContext.getCurrentInstance().addMessage(componentId, new FacesMessage(severity, summary, detail));
    }

    protected String getMessageForKey(String key) {
        FacesContext ctx = FacesContext.getCurrentInstance();
        ResourceBundle rb = ctx.getApplication().getResourceBundle(ctx, "i18n");
        return rb.getString(key);
    }

    protected FacesMessage getFacesMessageForKey(String key) {
        return new FacesMessage(getMessageForKey(key));
    }

    protected Logger getLogger(Class<?> clazz) {
        if (clazz == null) {
            throw new IllegalArgumentException("Class for logger is required.");
        }
        return Logger.getLogger(clazz.getName());
    }

    protected String getUrlParm(String parmName) {
        String value = null;
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        Map parms = context.getRequestParameterMap();
        if (parms != null) {
            value = (String) parms.get(parmName);
        }
        return value;
    }

    protected void print(String message) {
        System.out.println(message);
    }

    protected void addCallbackParam(String paramName, Object paramValue) {
        RequestContext context = RequestContext.getCurrentInstance();
        context.addCallbackParam(paramName, paramValue);
    }

    public FacesContext getFacseContext() {
        return FacesContext.getCurrentInstance();
    }

    public Object getBean(String name) {
        return getFacseContext().getApplication().getELResolver().getValue(getFacseContext().getELContext(), null, name);
    }
}
